package com.nandi.deviceinstall.presenter

import com.nandi.deviceinstall.base.BasePresenter
import com.nandi.deviceinstall.bean.BaseObjectBean
import com.nandi.deviceinstall.bean.LoginBean
import com.nandi.deviceinstall.contract.EquimentStepContract
import com.nandi.deviceinstall.contract.LoginContract
import com.nandi.deviceinstall.contract.UploadSuccessEquipmentContract
import com.nandi.deviceinstall.model.EquipmentStepModel
import com.nandi.deviceinstall.model.LoginModel
import com.nandi.deviceinstall.model.UploadSuccessEquipmentModel
import com.nandi.deviceinstall.net.BaseSubscriber
import com.nandi.deviceinstall.net.RxScheduler
import com.uber.autodispose.FlowableSubscribeProxy
import okhttp3.ResponseBody
import org.reactivestreams.Subscription


class UploadSuccessEquipmentPresenter : BasePresenter<UploadSuccessEquipmentContract.View>(), UploadSuccessEquipmentContract.Presenter {

    private val model: UploadSuccessEquipmentContract.Model

    init {
        model = UploadSuccessEquipmentModel()
    }

    override fun requestDownPic(token: String, path: String) {
        var fileName = path.substring(path.lastIndexOf("/")+1)
        if (!isViewAttached) {
            return
        }
        mView!!.showLoading()
        model.executeDownPic(token,path)
                .compose(RxScheduler.Flo_io_main())
                .`as`<FlowableSubscribeProxy<ResponseBody>>(mView!!.bindAutoDispose())
                .subscribe(object : BaseSubscriber<ResponseBody>(mView!!) {
                    override fun onNext(t: ResponseBody) {
                        //mView!!.uploadPicSuccess(t)
                        mView!!.downPicSuccess(t,fileName)
                    }

                    override fun onSubscribe(s: Subscription?) {
                        s!!.request(1)
                    }

                    override fun onError(e: Throwable?) {
                        super.onError(e)
                    }
                })
    }
}

